﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using QT.Windows.Shell;

namespace CloseWindow
{
    public partial class Form1 : GlassForm
    {
        #region Attributes

        DateTime CloseTime; //关机时间
        TimeSpan diffTime;
        bool IsExist = false;

        #endregion

        ExitWindows objExitWindows;

        #region Form

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DateTime DateTime = DateTime.Now;

            this.dtDate.Value = DateTime;
            this.dtTime.Value = DateTime;
            this.btnCancel.Enabled = false;

            objExitWindows = new ExitWindows();

            CheckSelect();
        }

        /// <summary>
        /// 关闭按钮最小化到通知区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;

            if (IsExist)
            {
                e.Cancel = false;
                return;
            }

            if (DialogResult.OK == MessageBox.Show("确认要退出吗", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                e.Cancel = false;
            }
        }

        /// <summary>
        /// 调整窗口大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
        }

        #endregion

        #region 定时关机

        #region RadioButton

        /// <summary>
        /// 切换关机定时类型
        /// </summary>
        private void CheckSelect()
        {
            if (this.rBclose1.Checked)
            {
                this.dtDate.Enabled = true;
                this.dtTime.Enabled = true;
                this.tbCloseTime.Enabled = false;
            }
            if (this.rBclose2.Checked)
            {
                this.dtDate.Enabled = false;
                this.dtTime.Enabled = false;
                this.tbCloseTime.Enabled = true;
            }
        }

        private void rBclose1_CheckedChanged(object sender, EventArgs e)
        {
            CheckSelect();
        }

        private void rBclose2_CheckedChanged(object sender, EventArgs e)
        {
            CheckSelect();
        }

        #endregion

        #region Button

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (this.rBclose1.Checked)
            {
                int Year = this.dtDate.Value.Year;
                int Month = this.dtDate.Value.Month;
                int Day = this.dtDate.Value.Day;
                int Hour = this.dtTime.Value.Hour;
                int Minute = this.dtTime.Value.Minute;
                int Second = this.dtTime.Value.Second;

                CloseTime = new DateTime(Year, Month, Day, Hour, Minute, Second);
                                
                if ((CloseTime-DateTime.Now).TotalSeconds < 0)
                {
                    MessageBox.Show("设定的关机时间必须大于当前时间");
                    return;
                }

                if ((CloseTime - DateTime.Now).TotalSeconds < 60)
                {
                    MessageBox.Show("设定的关机时间与当前时间差异不能小于60秒");
                    return;
                }
            }

            if (this.rBclose2.Checked)
            {
                if (!Regex.IsMatch(this.tbCloseTime.Text, @"^\d*$"))
                {
                    MessageBox.Show("倒计时仅支持正整数");
                    return;
                }

                CloseTime = DateTime.Now.AddMinutes(Convert.ToInt32(this.tbCloseTime.Text));
            }

            this.btnStart.Enabled = false;
            this.btnCancel.Enabled = true;
            this.timer1.Interval = 1000;
            this.timer1.Start();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
            this.timer1.Dispose();
            this.lblReserve.Text = "任务已取消";
            this.btnStart.Enabled = true;
            this.btnCancel.Enabled = false;
        }

        #endregion

        private void timer1_Tick(object sender, EventArgs e)
        {
            diffTime = CloseTime - DateTime.Now;

            if (diffTime.TotalSeconds < 61 && this.rBWarn2.Checked == true)
            {
                Warning Warning = new Warning(this);
                this.timer1.Stop();
                this.Visible = false;
                Warning.Show();
            }

            if (diffTime.TotalSeconds < 0 && this.rBWarn2.Checked == false)
            {
                bool ok = objExitWindows.ShutDown();
                if (!ok)
                {
                    MessageBox.Show("无法完成操作");
                }
            }
            this.lblReserve.Text = diffTime.Days + "天" + diffTime.Hours + "小时" + diffTime.Minutes + "分" + diffTime.Seconds + "秒";
        }

        #endregion 

        #region 立即操作

        private void btnReLogin_Click(object sender, EventArgs e)
        {
            if (!(DialogResult.OK == MessageBox.Show("您确认要注销当前用户吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)))
            {
                return;
            }

            IsExist = true;

            bool ok = objExitWindows.LogOff();
            if (!ok)
            {
                MessageBox.Show("无法完成操作");
            }
        }

        private void btnReBoot_Click(object sender, EventArgs e)
        {
            if (!(DialogResult.OK == MessageBox.Show("您确认要重新启动计算机吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)))
            {
                return;
            }

            IsExist = true;

            bool ok = objExitWindows.ReBoot();
            if (!ok)
            {
                MessageBox.Show("无法完成操作");
            }
        }

        private void btnShutDown_Click(object sender, EventArgs e)
        {
            if (!(DialogResult.OK == MessageBox.Show("您确认要关闭计算机吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)))
            {
                return;
            }

            IsExist = true;

            bool ok = objExitWindows.ShutDown();
            if (!ok)
            {
                MessageBox.Show("无法完成操作");
            }
        }

        #endregion

        #region NotifyIcon&ContextMenu

        private void MenuItem_Main_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void MenuItem_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        #endregion
    }
}
